package service;

import mapper.ArticleMapper;
import mapper.LoginMessageMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import po.Article;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "searchServiceImpl")
public class SearchServiceImpl implements SearchService{

    //日志文件类
    private static final Logger logger = LoggerFactory.getLogger(SearchServiceImpl.class);

    //自动注入数据库接口文件的代理类
    @Autowired
    private LoginMessageMapper loginMessageMapper;
    @Autowired
    private ArticleMapper articleMapper;


    @Override
    public Article getTheArticle(int id) throws Exception {
        Article TheArticle;

        TheArticle = articleMapper.searchId(id);

        return TheArticle;
    }

    @Override
    public List<Article> getThreeNewestArticles() throws Exception {

        List<Article> allArticles = new ArrayList<>();
        List<Article> resultArticles = new ArrayList<>();

        allArticles = articleMapper.searchAllArticles();

        int articlesNum = allArticles.size();

        //因为前端只需要三个，所以这里只取三个（因为在数据库中数据是按id排好的，所以取最后面三个即可）
        for (int i=articlesNum;i>articlesNum-3;i--){
            resultArticles.add(allArticles.get(i-1));
        }

        return resultArticles;

    }

    @Override
    public List<Article> getAllArticles() throws Exception {
        List<Article> resultArticles = new ArrayList<>();

        resultArticles = articleMapper.searchAllArticles();

        return resultArticles;
    }
}
